package com.example.himalaya.view_component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.himalaya.R;
import com.example.himalaya.base.BaseApplication;

/**
 * 管理UI状态的抽象类
 */
public abstract class UILoader extends FrameLayout {

    private OnRetryClickListener mOnRetryClickListener = null;


    // UI 处于的状态可能
    public enum UIStatus {
        LOADING, SUCCESS, ERROR, EMPTY, NULL
    }

    // Variable
    private View mLoadingView;
    private View mSuccessView;
    private View mNetErrorView;
    private View mEmptyView;

    // UI 处于的状态flag
    public UIStatus mCurrentStatus = UIStatus.NULL;

    public UILoader(@NonNull Context context) {
        this(context, null);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    // 初始化
    private void init() {
        switchUIByCurrentStatus();
    }


    /**
     * 根据UI状态flag切换UI
     */
    private void switchUIByCurrentStatus() {

        // 加载中
        if (mLoadingView == null) {
            mLoadingView = getLoadingView();
            addView(mLoadingView);
        }

        // 根据状态设置是否可见
        mLoadingView.setVisibility(mCurrentStatus == UIStatus.LOADING ? VISIBLE : GONE);

        // 成功
        if (mSuccessView == null) {
            mSuccessView = getSuccessView(this);
            addView(mSuccessView);
        }

        // 根据状态设置是否可见
        mSuccessView.setVisibility(mCurrentStatus == UIStatus.SUCCESS ? VISIBLE : GONE);

        // 网络错误页面
        // 加载中
        if (mNetErrorView == null) {
            mNetErrorView = getNetworkErrorView();
            addView(mNetErrorView);
        }
        mNetErrorView.setVisibility(mCurrentStatus == UIStatus.ERROR ? VISIBLE : GONE);

        // 数据为空的界面
        if (mEmptyView == null) {
            mEmptyView = getEmptyView();
            addView(mEmptyView);
        }
        mEmptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY ? VISIBLE : GONE);
    }


    // 获取各种的UI界面的方法
    protected abstract View getSuccessView(ViewGroup container);

    private View getLoadingView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading_view, this, false);
    }

    protected View getNetworkErrorView() {

        // 网络错误的图标
        View netWorkErrorView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_error_view, this, false);

        // 给图标注册点击刷新的事件
        netWorkErrorView.findViewById(R.id.network_error_icon);
        netWorkErrorView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // 重新获取数据
                if (mOnRetryClickListener != null) {
                    mOnRetryClickListener.onRetryClick();
                }
            }
        });


        return netWorkErrorView;
    }

    protected View getEmptyView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);
    }

    /**
     * 更新UI状态的方法
     */
    public void updateStatus(UIStatus status) {
        mCurrentStatus = status;

        // 跟新UI 一定要在主线程
        BaseApplication.getsHandler().post(new Runnable() {
            @Override
            public void run() {
                switchUIByCurrentStatus();
            }
        });
    }

    public void setOnRetryClickListener(OnRetryClickListener listener) {
        this.mOnRetryClickListener = listener;
    }

    public interface OnRetryClickListener {
        void onRetryClick();
    }
}
